import React, { useEffect, useState } from "react";

import { Button, Table, Space, Switch, message, } from "antd";
import api from '../../../../api'
import ThreeLevelinkage from '../../../../component/ThreeLevelinkage/index'

const Lease = () => {
  const [data, setData] = useState([])

  // dom渲染后 componentDidMount
  useEffect(() => {
    getData()
  }, [])

  const getData = async () => {
    const { data: res } = await api.getLeaseTable()
    res.data = res.data.filter(item => {
      return item.key = item.CourseID
    })
    setData([...res.data])
  }
  // table表头
  const columns = [
    {
      title: "序号",
      dataIndex: "CourseID",
      key: "CourseID",
      align: "center",
    },
    {
      title: "楼盘名称",
      dataIndex: "CourseName",
      key: "CourseName",
      align: "center",
    },
    {
      title: "详细地址",
      dataIndex: "CourseAddress",
      key: "CourseAddress",
      align: "center",
    },
    {
      title: "租金",
      key: "CoursePrice",
      dataIndex: "CoursePrice",
      align: "center",
    },
    {
      title: "排序",
      dataIndex: "sort",
      key: "sort",
      align: "center",
    },
    {
      title: "更新时间",
      dataIndex: "CourseTime",
      key: "CourseTime",
      align: "center",
    },
    {
      title: "状态",
      dataIndex: "status",
      key: "status",
      align: "center",
      render: (text) => (
        <Switch
          checkedChildren="正常"
          unCheckedChildren="异常"
          defaultChecked={text}
        />
      ),
    },
    {
      title: "经纪人",
      key: "CourseRoot",
      dataIndex: "CourseRoot",
      align: "center",
    },
    {
      title: "操作",
      key: "action",
      align: "center",
      render: (text, record) => (
        <Space size="middle">
          <Button type="link" onClick={() => { }}>
            详情
          </Button>
          <Button type="link">编辑</Button>
          <Button type="link" onClick={() => { delItem(record.CourseID) }}>删除</Button>
          <Button type="link">指派经纪人</Button>
        </Space>
      ),
    },
  ];
  // 删除
  const delItem = async (CourseID) => {
    const { data: res } = await api.delLeaseItem({ CourseID })
    message.success(res.msg)
    console.log(res);
    getData()
  }
  // 父子传参---模糊搜索
  const result1 = (res) => {
    console.log(res);
    res.data.data = res.data.data.filter(item => {
      return item.key = item.CourseID
    })

    setData([...res.data.data])
  }
  return (
    <div className="sale">
      <ThreeLevelinkage result1={result1} />
      <div className="sale_main">
        <Button type="primary">新增</Button>
        <Table columns={columns} dataSource={data} />
      </div>
    </div>
  );
};

export default Lease;